package com.example.tliaswebmanagement.controller;

import com.example.tliaswebmanagement.entity.Dept;
import com.example.tliaswebmanagement.entity.Result;
import com.example.tliaswebmanagement.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 部门管理控制器
 */
@Slf4j
@RestController
public class DeptController {
    //创建一个log来获取打印日志，也可以直接在上面加注解@Slf4j
    //private static final Logger log= LoggerFactory.getLogger(DeptController.class);

    private final DeptService deptservice;
    @Autowired
    public DeptController(DeptService deptService) {
        this.deptservice = deptService;
    }

    //@RequestMapping("/depts")//任何请求方式都可
    //下面两种方法来限制请求方式
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping("/depts")//recommond the second,this is clean!
    public Result list(){
        List<Dept> depts=deptservice.findAll();
        log.info("获取部门全部列表");
        return Result.success(depts);
    }

    @DeleteMapping("/depts")
    public Result delete(Integer id){
        deptservice.deleteById(id);
        return Result.success();
    }

    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        //System.out.println("新增部门:"+dept.getName());
        log.info("新增部门:"+dept.getName());
        deptservice.save(dept);
        return Result.success();
    }

    @GetMapping("/depts/{id}")
    public Result getById(@PathVariable Integer id){
//        System.out.println("修改部门:"+id);
        //可以用占位符来表示
        log.info("修改部门{}",id);
        Dept dept=deptservice.getById(id);
        return Result.success(dept);
    }

    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
//        System.out.println("修改部门名字为"+dept.getName());
        log.info("修改部门{}",dept);
        deptservice.update(dept);
        return Result.success();
    }
}
